
#include "hardware.h"
#include "sys.h"
#include "debug.h"
#include "macros.h"
#include "status.h"
#include "atd.h"

#include "FreeRTOS.h"
#include "task.h"

#define   ATD_CHAN1	0
#define   ATD_CHAN2	1
#define   ATD_CHAN3	5

unsigned char atd_debug_enable = 0;

void atd_init( void )
{
	PCONP |= PCAD;

	PINMODE1 |= (2UL<<14);			  /* P0.23 - Vin; no pull-up no pull-down */
//	PINMODE1 |= (2UL<<20);			  /* P0.26 - Vbat; no pull-up no pull-down */

	PINSEL1 |= (1UL<<14);		  /* ATD 0 P0.23 - Vin */
	/* PINSEL1 |= (1UL<<16);		  /\* ATD 1 P0.24 *\/ */
	/* PINSEL3 |= (3UL<<30);		  /\* ATD 5 P1.31 *\/ */

	AD0CR = ( 0x01 << 0 ) | 	/* SEL=1,select channel 0~7 on ADC0 */
		( ( PCLK / ADC_CLK -1 ) << 8 ) |  /* CLKDIV = Fpclk / 1000000 - 1 */ 
		( 0 << 16 ) | 		/* BURST = 0, no BURST, software controlled */
		( 0 << 17 ) |  		/* CLKS = 0, 11 clocks/10 bits */
		( 1 << 21 ) |  		/* PDN = 1, normal operation */
		( 0 << 22 ) |  		/* TEST1:0 = 00 */
		( 0 << 24 ) |  		/* START = 0 A/D conversion stops */
		( 0 << 27 );		/* EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) */ 

}

static unsigned int atd_read( unsigned char channelNum )
{
	unsigned int regVal, ADC_Data;

	channelNum &= 0x07;

	AD0CR &= 0xFFFFFF00;
	AD0CR |= (1 << 24) | (1 << channelNum);
	/* switch channel,start A/D convert */
	while ( 1 )			/* wait until end of A/D convert */
	{
		regVal = *(volatile unsigned long *)(AD0_BASE_ADDR + ADC_OFFSET + ADC_INDEX * channelNum);
		/* read result of A/D conversion */
		if ( regVal & ADC_DONE )
		{
			break;
		}
	}	
        
	AD0CR &= 0xF8FFFFFF;	/* stop ADC now */    
	if ( regVal & ADC_OVERRUN )	/* save data when it's not overrun, otherwise, return zero */
	{
		return ( 0 );
	}
	
	ADC_Data = ( regVal >> 6 ) & 0x3FF;

	return ( ADC_Data );	/* return A/D conversion value */
}



#define   ABUF_SIZE			20
unsigned short abuf[ABUF_SIZE];
unsigned char p = 0;
int mres = 0;

void atd_task ( void *pvParameters )
{
	int c1,i;
	unsigned int res;
	
	atd_init();

	portTickType xLastWakeTime = xTaskGetTickCount();

	srand(xLastWakeTime);

	/* unsigned portBASE_TYPE s =uxTaskGetStackHighWaterMark(NULL); */
	/* DBG("statck size: %d",s); //Dima */

	
	for(;;)
	{
		vTaskDelayUntil( &xLastWakeTime, ( 500 MSEC ));

		c1 = atd_read(ATD_CHAN1);
			
		abuf[p++] = c1;
		p %= ABUF_SIZE;

		res = 0;
		for ( i = 0; i < ABUF_SIZE; i++ )
		{
			res += abuf[i];
		}
			
		res /= ABUF_SIZE;

		if ( res != mres )
		{
			mres = res;
			dev_status.oil = res;
			if ( atd_debug_enable )
			{
				dbg("====== CH1: %d =======",res); //Dima

				res = rand();
				res %= 1023;
				if ( res < 260 )
					res += 260;
				dev_status.temperature = res; /* 260 - 1023 */
				dbg("=== dev_status.temperature: %d", dev_status.temperature); //Dima 
			}
			//dbg("=== 22222222222 === counter c:%d\n\r",55555);
		}


//		taskYIELD();
	}		
}


/* void atd_task ( void *pvParameters ) */
/* { */
/* 	int c1,c2,c3,i; */
/* 	unsigned int res; */
	
/* 	atd_init(); */

/* 	portTickType xLastWakeTime = xTaskGetTickCount(); */

/* 	/\* unsigned portBASE_TYPE s =uxTaskGetStackHighWaterMark(NULL); *\/ */
/* 	/\* DBG("statck size: %d",s); //Dima *\/ */

	
/* 	for(;;) */
/* 	{ */
/* 		vTaskDelayUntil( &xLastWakeTime, ( 500 MSEC )); */

/* 		c1 = atd_read(ATD_CHAN1); */
/* 		c2 = atd_read(ATD_CHAN2); */
/* 		c3 = atd_read(ATD_CHAN3); */
			
/* 		abuf[p++] = c1; */
/* 		p %= ABUF_SIZE; */

/* 		res = 0; */
/* 		for ( i = 0; i < ABUF_SIZE; i++ ) */
/* 		{ */
/* 			res += abuf[i]; */
/* 		} */
			
/* 		res /= ABUF_SIZE; */
			
/* 		if ( res != c1m ) */
/* 		{ */
/* 			c1m = res; */
/* 			dev_status.oil = res; */
/* 			//dbg("=== 22222222222 === counter c:%d\n\r",55555); */
/* 			if ( atd_debug_enable ) */
/* 				dbg("====== CH1: %d =======",res); //Dima */
/* 		} */

/* 		if ( c1 != mc1 ) */
/* 		{ */
/* 			//dbg("c1: %d", c1); //Dima */
/* 			mc1 = c1; */
/* 		} */

/* 		if ( c2 != mc2 ) */
/* 		{ */
/* 			dbg("c2: %d", c2); //Dima */
/* 			mc2 = c2; */
/* 		} */

/* 		if ( c3 != mc3 ) */
/* 		{ */
/* 			dbg("c3: %d", c3); //Dima */
/* 			mc3 = c3; */
/* 		} */

/* 		taskYIELD(); */
/* 	}		 */
/* } */
 

